package com.wangijun.yuan.admin.blog.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.core.domain.TagInfo;
import com.wangijun.yuan.core.domain.TopicInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import com.wangijun.yuan.admin.base.CommonPage;
import com.wangijun.yuan.admin.base.CommonResult;
import com.wangijun.yuan.core.model.IdParam;
import com.wangijun.yuan.core.model.IdsParam;
import io.swagger.v3.oas.annotations.Operation;
import com.wangijun.yuan.admin.blog.model.vo.TopicInfoVO;
import com.wangijun.yuan.admin.blog.model.dto.TopicInfoQryParam;
import com.wangijun.yuan.admin.blog.model.dto.TopicInfoSaveParam;
import com.wangijun.yuan.admin.blog.model.dto.TopicInfoUpdateParam;

/**
 * <p>
 * 话题表 前端控制器
 * </p>
 *
 * @author Wlf
 * @since 2024-08-26
 */
@Tag(name = "话题信息相关接口")
@RestController
@RequestMapping("/topicInfo")
public class TopicInfoController {

    private final static Class<TopicInfo> clz = TopicInfo.class;


    @Operation(summary = "查", description = "查")
    @PostMapping("/list")
    CommonResult<CommonPage<TopicInfoVO>> list(@RequestBody TopicInfoQryParam param) {
        return CommonResult.data(CommonPage.toPage(Db.lambdaQuery(clz)
                        .like(StrUtil.isNotBlank(param.getName()), TopicInfo::getName, param.getName())
                        .orderByDesc(TopicInfo::getId)
                        .page(param.page()),
                TopicInfoVO.class));
    }

    @Operation(summary = "增加", description = "增加")
    @PostMapping("/add")
    CommonResult<Boolean> add(@RequestBody @Validated TopicInfoSaveParam param) {
        Db.save(BeanUtil.copyProperties(param, clz));
        return CommonResult.data(true);
    }

    @Operation(summary = "ByID获取", description = "ByID获取")
    @PostMapping("/get")
    CommonResult<TopicInfoVO> get(@RequestBody @Validated IdParam param) {
        return CommonResult.data(BeanUtil.copyProperties(Db.getById(param.getId(), clz), TopicInfoVO.class));

    }

    @Operation(summary = "改", description = "改")
    @PostMapping("/update")
    CommonResult<Boolean> update(@RequestBody @Validated TopicInfoUpdateParam param) {
        var item = BeanUtil.copyProperties(param, clz);
        Db.updateById(item);
        return CommonResult.data(true);
    }

    @Operation(summary = "删", description = "删")
    @PostMapping("/del")
    CommonResult<Boolean> del(@RequestBody IdParam param) {
        Db.removeById(param.getId(), clz);
        return CommonResult.data(true);
    }

    @Operation(summary = "删", description = "删")
    @PostMapping("/delBatch")
    CommonResult<Boolean> delBatch(@RequestBody IdsParam param) {
        Db.removeByIds(param.getIds(), clz);
        return CommonResult.data(true);
    }
}
